package com.jiezuo.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.auth.entity.Position;
import com.jiezuo.auth.entity.User;
import com.jiezuo.auth.mapper.PositionMapper;
import com.jiezuo.auth.service.PositionService;
import com.jiezuo.auth.service.UserService;
import com.jiezuo.common.util.StringUtil;
import com.jiezuo.common.util.VerifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description 岗位
 * @author 庞留杰
 * @date 2024/03/01 15:03
 * @version V1.0
 **/
@Transactional
@Slf4j
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {

    @Autowired
    PositionMapper mapper;
    @Autowired
    UserService userService;

    @Override
    public IPage<Position> findPage(Page initMpPage, Position position) {
        IPage<Position> page = mapper.findList(initMpPage, position);
        return page;
    }

    @Override
    public List<Position> findList(Position position) {
        List<Position> list = mapper.findList(position);
        return list;
    }

    @Override
    public Position findById(String id) {
        return this.getById(id);
    }

    @Override
    public boolean add(Position position) {
        boolean save = this.save(position);
        dealUser(position);
        return save;
    }

    @Override
    public boolean update(Position position) {
        boolean update = this.updateById(position);
        dealUser(position);
        return update;
    }

    void dealUser(Position position) {
        String id = position.getId();
        String users = position.getUsers();
        // 删除旧的关联关系
        List<User> deleteList = mapper.findUserListByPositionId(id);
        if (deleteList.size() > 0) {
            for (User user : deleteList) {
                String[] position_arr = user.getPositions().split(",");
                // 过滤掉指定值并重新拼接
                System.out.println("user.getPositions() = " + user.getPositions());
                String result = Arrays.stream(position_arr).filter(item -> !item.equals(id)).collect(Collectors.joining(","));
                System.out.println("result = " + result);
                result = StringUtil.isNotEmpty(result) ? result : null;
                // 更新
                UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", user.getId());
                updateWrapper.set("positions", result);
                userService.update(user, updateWrapper);
            }
        }
        // 增加新的关联关系
        if (StringUtil.isNotEmpty(users)) {
            String[] split = users.split(",");
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_delete", "0");
            queryWrapper.in("id", split);
            List<User> addList = userService.list(queryWrapper);
            for (User user : addList) {
                String result = "";
                if (StringUtil.isNotEmpty(user.getPositions())) {
                    String[] position_arr = user.getPositions().split(",");
                    HashSet<String> strings = new HashSet<>(Arrays.asList(position_arr));
                    strings.add(id);
                    result = String.join(",", strings);
                } else {
                    result = id;
                }
                // 更新
                UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", user.getId());
                updateWrapper.set("positions", result);
                userService.update(user, updateWrapper);
            }
        }
    }

    @Override
    public boolean delete(String id) {
        Position position = new Position();
        position.setId(id);
        position.setIs_delete("1");
        return this.updateById(position);
    }

    @Override
    public List<User> findUserListByPositionId(String position_id) {
        VerifyUtil.notEmpty("岗位ID", position_id);
        return mapper.findUserListByPositionId(position_id);
    }

}
